<template>
    <div>
      <Header>
        <a @click="goLibrary" style="color:#F60;line-height: 52px;">作品库</a>
      </Header>
      <div style="margin-top:-20px;">
         <group>
            <selector title="所属卷" placeholder="请选卷" v-model="addForm.volumeId" :options="volumeList" readonly></selector>
            <x-input title="章节名" v-model="addForm.name" placeholder="请设置章节名" disabled ></x-input>
            <x-textarea title="内容" v-model="addForm.content" :max="10000" :rows="20" placeholder="作品第一章需要上传五百字以上的内容，才能保障您的作品顺利通过审核。首章字数低于500字无法被审核。祝您创作愉快！"></x-textarea>
            <div style="padding:20px"><x-button type="warn" @click.native="fabu">保存修改</x-button></div>
         </group>
      </div>
      <divider>37K小说网 创作改变人生</divider>
    </div>
</template>

<script>
import Header from '@/components/commons/header/header'
import bookController from '@/api/bookController/index'
import USER from '@/utils/user'
import {mapActions,mapState} from 'vuex'
    export default {
        name:'updateChapter',
          data(){
            return {
              userInfo: null,
              classifyList:[],//分类
              addForm:{
                id:null,//章节id
                bookId:null,//书籍id
                name:null,//章节名
                bookName:null,
                name:null, //章节名
                volumeId:null,
                path:null,
                content:null//章节内容
              }
            }
          },
          created(){
            this.getLoginInfo();
            if(JSON.stringify(this.$route.params)=='{}'){
              this.$router.go(-1);
              return false
            }
            bookController.getChapterInfoById({id:this.$route.params.id}).then(response=>{
                this.addForm.bookId = response.data.bookId;
                this.addForm.bookName = response.data.bookName;
                this.addForm.id = response.data.id;
                this.addForm.name = response.data.name;
                this.addForm.volumeId = response.data.volumeId;
                this.addForm.content = response.data.content;
                this.addForm.path = response.data.path;
            }) 
            this.getVolume();//获取频道
          },
          computed: {
            ...mapState({
              volumeList: state => state.volume.volumeList,
            })
          },
          components:{Header},
          methods:{
            ...mapActions([
              'setVolumeList'
            ]),
            getVolume(){//获取频道
              if(this.volumeList.length==0){
                bookController.getAllVolume().then(response=>{
                  var list = [];
                  for(var i in response.data.list){
                    var res = {
                      key:null,
                      value:null
                    }
                    res.key = response.data.list[i].id;
                    res.value = response.data.list[i].name;
                    list.push(res);
                  }
                  this.setVolumeList(list)
                })
              }
            },
            getLoginInfo(){ //获取登录信息,如果登录，重定向至登录页面
              var loginInfo = USER.getUserInfo();
              this.userInfo = loginInfo;
              if(loginInfo.isLogin==1){ //0已登录 1未登录
                this.$router.push({name:'login'})
              }else if(!loginInfo.userCustominfo5){//不存在笔名，非作者，跳转至创建笔名页面
                this.$router.push({name:'penName'})
              }
            },
            goLibrary(){
              this.$router.push({name:'library'})
            },
            fabu(){ //发布章节
              var formData = {
                id:this.addForm.id,
                path:this.addForm.path,
                content:this.addForm.content
              }
              bookController.updateChapterContent(formData).then(response=>{
                //章节创建成功
                if(response.success){
                  this.$vux.toast.show({
                    text: response.msg,
                    type:'success'
                  })
                  //跳转至作品管理
                  this.$router.push({name:'getManageBookPage',params:this.addForm});
                }else{
                  this.$vux.toast.show({
                    text: response.msg,
                    type:'warn'
                  })
                }
              })
            }
          }
    }
</script>

<style scoped>

</style>
